/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.classdiagram.parsers.texteditor.operationlabel;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferenceConverter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Display;

import com.cea.papyrus.classdiagram.parsers.PapyrusParsersPlugin;
import com.cea.papyrus.classdiagram.parsers.preferences.IPreferencesConstants;

/**
 * Color provider to syntax highlight.
 */
public class OperationLabelColorProvider implements IOperationLabelColorConstants {
    
    /**
     * 
     */
    private static OperationLabelColorProvider instance;
    
    /**
     * 
     */
    private OperationLabelColorProvider() {
    }
    
    /**
     * 
     * 
     * @return 
     */
    public static OperationLabelColorProvider getDefault() {
        if(instance==null) {
            instance = new OperationLabelColorProvider();
        }
        return instance;
    }
    
    /**
     * 
     */
    protected Map<String, Color> fColorTable = new HashMap<String, Color>(10);
    
    /**
     * Set default colors in given preference store.
     * 
     * @param aStore the pref store
     */
    public static void initializeDefaults(IPreferenceStore aStore) {
        PreferenceConverter.setDefault(aStore,
                IPreferencesConstants.COLOR_DEFAULT, IOperationLabelColorConstants.RGB_DEFAULT);
        PreferenceConverter.setDefault(aStore,
                IPreferencesConstants.COLOR_KEYWORD, IOperationLabelColorConstants.RGB_KEYWORD);
        PreferenceConverter.setDefault(aStore,
                IPreferencesConstants.COLOR_STRING, IOperationLabelColorConstants.RGB_STRING);
    }
    
    /**
     * Returns specified color that is stored in the color table. If color not
     * found in color table then a new instance is created from according
     * preferences value and stored in color table.
     * 
     * @param aName the name of the color
     * 
     * @return the color instance
     */
    public Color getColor(String aName) {
        Color color = fColorTable.get(aName);
        if (color == null) {
            IPreferenceStore store =
                PapyrusParsersPlugin.getDefault().getPreferenceStore();
            RGB rgb = PreferenceConverter.getDefaultColor(store,
                    IPreferencesConstants.PREFIX_COLOR + aName);
            if (rgb != null) {
                color = new Color(Display.getCurrent(), rgb);
            } else {
                color = Display.getCurrent().getSystemColor(
                        SWT.COLOR_LIST_FOREGROUND);
                PapyrusParsersPlugin.logError("Undefined color '" +
                        aName + "'");
            }
            fColorTable.put(aName, color);
        }
        return color;
    }
    
    /**
     * Release all of the color resources held onto by the color provider.
     */
    public void dispose() {
        Iterator colors = fColorTable.values().iterator();
        while (colors.hasNext()) {
            ((Color)colors.next()).dispose();
        }
    }
}